<template>
  <div class="medical_institution_index">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dUpMain"></div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="this.$route.query.Token ? 'top:50px;left:10px' : 'top: 100px'"
        >
          <div style="margin: 15px">
            <span>科室名称：</span>
            <el-input
              style="display: inline-block; width: 15%"
              size="small"
              v-model="value"
            ></el-input>
            <el-button
              style="margin-left: 15px"
              size="small"
              icon="el-icon-search"
              @click="(page.page = 1), getTableData()"
            ></el-button>
            <el-button
              style="margin-left: 15px"
              size="small"
              icon="el-icon-plus"
              @click="open"
            ></el-button>
          </div>
          <div>
            <template>
              <el-table :data="tableData" style="width: 100%" height="600">
                <el-table-column label="序号" type="index"> </el-table-column>
                <el-table-column prop="Code" label="科室代码">
                </el-table-column>
                <el-table-column prop="TelCategory" label="联系电话-类别">
                </el-table-column>
                <!-- <el-table-column
                  prop="TelCategoryCode"
                  label="联系电话-类别代码"
                >
                </el-table-column> -->
                <el-table-column prop="Telphone" label="联系电话-号码">
                </el-table-column>
                <el-table-column prop="Email" label="电子邮件地址">
                </el-table-column>
                <el-table-column prop="Organization" label="机构名称">
                </el-table-column>
                <!-- <el-table-column
                  prop="OrganizationCode"
                  label="机构组织机构代码"
                >
                </el-table-column> -->
                <el-table-column
                  prop="OrganizationLegal"
                  label="机构负责人（法人）"
                >
                </el-table-column>
                <el-table-column prop="OrganizationAddress" label="机构地址">
                </el-table-column>
                <el-table-column prop="Name" label="科室名称">
                </el-table-column>
                <el-table-column prop="OrganizationRole" label="机构角色">
                </el-table-column>
                <!-- <el-table-column
                  prop="OrganizationRoleCode"
                  label="机构角色代码"
                >
                </el-table-column> -->
                <el-table-column prop="CreateTime" label="创建时间">
                  <template slot-scope="scope">
                    {{ scope.row.CreateTime | dateFormat }}
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      icon="el-icon-edit"
                      @click="edit_item(scope.row)"
                    ></el-button>
                    <el-button
                      size="mini"
                      icon="el-icon-delete"
                      @click="del_item(scope.row)"
                    ></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </div>
          <div style="text-align: center" class="block">
            <el-pagination
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              @current-change="page_change"
              :current-page="page.page"
            >
            </el-pagination>
          </div>
          <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="45%"
            :before-close="handleClose"
            :append-to-body="true"
            :close-on-click-modal="false"
          >
            <el-form
              label-position="right"
              :model="formLabelAlign"
              label-width="140px"
            >
              <el-form-item
                label="科室代码"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.Code"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="联系电话-类别代码"
                style="display: inline-block; width: 50%"
              >
                <el-select
                  filterable
                  style="width: 100%"
                  v-model="formLabelAlign.TelCategoryCode"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options_Tel"
                    :key="item.value"
                    :label="item.Name"
                    :value="item.Code"
                    @click.native="options_Tel_change(item)"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label="联系电话-号码"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.Telphone"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="电子邮件地址"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.Email"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构名称"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.Organization"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构组织机构代码"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.OrganizationCode"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构负责人(法人)"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.OrganizationLegal"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构地址"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.OrganizationAddress"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="科室名称"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.Name"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构角色"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.OrganizationRole"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="机构角色代码"
                style="display: inline-block; width: 50%"
              >
                <el-input
                  style="display: inline-block; width: 100%"
                  v-model="formLabelAlign.OrganizationRoleCode"
                ></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose">取 消</el-button>
              <el-button type="primary" @click="btn_yes">确 定</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      value: "",
      tableData: [],
      page: {
        page: 1,
        limit: 10,
        total: 1,
      },
      dialogVisible: false,
      formLabelAlign: {
        TelCategoryCode: "",
        TelCategory: "",
      },
      title: "",
      value: "",
      options_Tel: [],
    };
  },
  methods: {
    getTableData() {
      this.$post(
        "/HospitalDepart/PageList?keyword=" +
          this.value +
          "&page=" +
          this.page.page +
          "&limit=" +
          this.page.limit
      ).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.page.total = res.data.TotalCount;
        } else if (res.code == -1) {
          this.$message.error(res.msg);
        }
      });
    },
    page_change(val) {
      this.page.page = val;
      this.getTableData();
    },
    open() {
      this.dialogVisible = true;
      this.formLabelAlign.Id ? (this.title = "修改") : (this.title = "新增");
    },
    handleClose() {
      this.dialogVisible = false;
      this.formLabelAlign = {
        TelCategoryCode: "",
        TelCategory: "",
      };
    },
    btn_yes() {
      console.log(this.formLabelAlign);
      let body = this.formLabelAlign;
      let url = this.formLabelAlign.Id
        ? "/HospitalDepart/Update"
        : "/HospitalDepart/Create";
      this.$post(url, body).then((res) => {
        if (res.code == 0) {
          this.getTableData();
          this.handleClose();
          this.$message({
            message: "操作成功",
            type: "success",
          });
        } else if (res.code == -1) {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    edit_item(val) {
      var info = JSON.stringify(val);
      this.formLabelAlign = JSON.parse(info);
      this.open();
    },
    del_item(val) {
      let id = val.Id;
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((_) => {
          this.$post("/HospitalDepart/Delete/" + id).then((res) => {
            if (res.code == 0) {
              this.getTableData();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            }
          });
        })
        .catch((_) => {});
    },
    getOptions_Tel() {
      this.$get("/PlatformRange/RangeList?fileCode=CV0400.01").then((res) => {
        if (res.code == 0) {
          this.options_Tel = res.data;
        }
      });
    },
    options_Tel_change(val) {
      // console.log(val);
      this.formLabelAlign.TelCategory = val.Name;
      this.formLabelAlign.TelCategoryCode = val.Code;
    },
  },
  mounted() {
    this.getTableData();
    // document.getElementById("2").focus();
    this.getOptions_Tel();
  },
  created() {
    if (this.$route.query.Token) {
      localStorage.setItem("Token", this.$route.query.Token);
    }
    this.getTableData();
    // document.getElementById("2").focus();
    this.getOptions_Tel();
  },
  filters: {
    dateFormat(val) {
      if (val == null || val == "") {
        return "";
      } else {
        let d = new Date(val); // val 为表格内取到的后台时间
        let month =
          d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
        let day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
        let hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
        let min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
        let sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
        let times =
          d.getFullYear() +
          "-" +
          month +
          "-" +
          day +
          " " +
          hours +
          ":" +
          min +
          ":" +
          sec;
        return times;
      }
    },
  },
};
</script>
<style lang="scss">
.medical_institution_index {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
}
</style>
